package 二叉树层序遍历.二叉树的最大深度;

import java.util.ArrayDeque;

public class Solution {
    public int maxDepth(TreeNode root) {

        if (root == null)
            return 0;

        ArrayDeque<TreeNode> queue = new ArrayDeque<>();
        queue.offer(root);
        TreeNode p, last = root;
        int level = 0;
        while (!queue.isEmpty()) {
            p = queue.poll();
            if (p.left != null) queue.offer(p.left);
            if (p.right != null) queue.offer(p.right);
            if (last == p) {
                last = queue.peekLast();
                level++;
            }
        }
        return level;
    }
}
